package com.example.jdbcdemo.util;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;

/**
 * 多线程异步执行工具类
 *
 * @author linzk
 * @version 1.0
 */
public class AsyncUtil {

    /**
     * 执行异步
     *
     * @param task 任务
     * @return {@link CompletableFuture}<{@link ?}>
     */
    public static CompletableFuture<?> execAsync(Runnable task) {
        return CompletableFuture.runAsync(task);
    }

    /**
     * 执行异步
     *
     * @param task 任务
     * @return {@link CompletableFuture}<{@link ?}>
     */
    public static CompletableFuture<?> execAsync(Runnable task, ExecutorService executorService) {
        return CompletableFuture.runAsync(task, executorService);
    }

    /**
     * 执行异步
     *
     * @param supplier 供应商
     * @return {@link CompletableFuture}<{@link T}>
     */
    public static <T> CompletableFuture<T> execAsync(Supplier<T> supplier) {
        return CompletableFuture.supplyAsync(supplier);
    }

    /**
     * 执行异步
     *
     * @param supplier 供应商
     * @return {@link CompletableFuture}<{@link T}>
     */
    public static <T> CompletableFuture<T> execAsync(Supplier<T> supplier, ExecutorService executorService) {
        return CompletableFuture.supplyAsync(supplier, executorService);
    }

    /**
     * 所有
     *
     * @param futures 期货
     * @return {@link CompletableFuture}<{@link Void}>
     */
    public static <T> CompletableFuture<Void> allOf(List<CompletableFuture<T>> futures) {
        return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
    }

}
